﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ATW.MT.BUS;
using System.Data;

namespace ATW.MT.SellManage
{
    public partial class ProductionDesc : System.Web.UI.Page
    {
        bus_T_ProductionArgs TP = new bus_T_ProductionArgs();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadLan();
            }
        }
        private void BindData()
        {//加载
            if (!String.IsNullOrEmpty(Request.QueryString["pid"]))
            {
                string ProductionID = Request.QueryString["pid"];
                string Name = Request.QueryString["n"];
                hdfProid.Value = ProductionID;
                lblProductionName.Text = Name;
                try
                {
                    DataTable dt = TP.getProductionDesc(ProductionID);
                    rpList.DataSource = dt;
                    rpList.DataBind();
                }
                catch (Exception ex)
                {

                }
            }
        }
        protected void lbnEdit_Click(object sender, EventArgs e)
        {//编辑
            LinkButton lbn = (LinkButton)sender;
            Label lblDesc = (Label)lbn.Parent.FindControl("lblDesc");
            TextBox txtDesc = (TextBox)lbn.Parent.FindControl("txtDesc");
            LinkButton lbnSave = (LinkButton)lbn.Parent.FindControl("lbnSave");
            if (lblDesc != null && txtDesc != null)
            {
                lblDesc.Visible = false;
                txtDesc.Visible = true;
                lbnSave.Visible = true;
                lbn.Visible = false;
                txtDesc.Text = lblDesc.Text;
            }
        }
        protected void lbnSave_Click(object sender, EventArgs e)
        {//保存
            LinkButton lbn = (LinkButton)sender;
            TextBox txtDesc = (TextBox)lbn.Parent.FindControl("txtDesc");
            string Lanid = lbn.CommandArgument;
            string productionID = hdfProid.Value;
            try
            {
                string desc = txtDesc.Text.Trim();
                if (desc == "")
                {
                    Tips("产品描述不能为空！");
                    return;
                }
                if (!CommonClass.common.filterChar(desc))
                {
                    Tips("产品描述中含有危险字符！");
                    return;
                }
                int result = TP.editProductionDesc(productionID, Lanid, desc);
                if (result > 0)
                {
                    Tips("修改成功！");
                    BindData();
                }
                else
                {
                    Tips("修改失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnSaveNew_Click(object sender, EventArgs e)
        {
            try
            {
                string productionID = hdfProid.Value;
                string lanID = ddlLan.SelectedValue;
                string Desc = txtNewDesc.Text.Trim();
                if (Desc == "")
                {
                    Tips("产品名称不能为空！");
                    return;
                }
                if (!CommonClass.common.filterChar(Desc))
                {
                    Tips("产品名称中含有危险字符！");
                    return;
                }
                if (TP.isProductionDescExist(productionID, lanID))
                {
                    Tips("产品描述已经存在！");
                    return;
                }
                int result = TP.addProductionDesc(productionID, lanID, Desc);
                if (result > 0)
                {
                    Tips("新增成功！");
                    BindData();
                    txtNewDesc.Text = "";
                }
                else
                {
                    Tips("新增失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnAdd_Click(object sender, EventArgs e)
        {
            if (!trAdd.Visible)
            {
                trAdd.Visible = true;
                txtNewDesc.Text = "";
            }
        }
        private void LoadLan()
        {
            try
            {
                DataTable dt = TP.getLanList();
                ddlLan.DataSource = dt;
                ddlLan.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}